<?php
namespace app\controller\api\v1\housing;

use app\Request;
use think\facade\Db;

class AnAppointment
{
	public function an_appointment_add(Request $request)
	{
		$user_id= input('param.user_id');
		$housing_id = input('param.housing_id');
		$ctime = input('param.ctime');


		$today_status=Db::name ('an_appointment')->where ('yvyue_user_id',$request->uid())-> whereTime('ctime', $ctime)->where ('housing_id',$housing_id)->where ('status',2)->count ();
		if ($today_status>=3){
			return app('json')->fail('最多可重复预约3次');
		}
		$today_status=Db::name ('an_appointment')->where ('yvyue_user_id',$request->uid())-> whereTime('ctime', $ctime)->where ('housing_id',$housing_id)->where ('status',0)->count ();
		if ($today_status>=1){
			return app('json')->fail('已经预约过了,请等待');
		}
		$today_statuss=Db::name ('an_appointment')->where ('yvyue_user_id',$request->uid())-> whereTime('ctime', $ctime)->where ('housing_id',$housing_id)->where ('status',1)->count ();
		if ($today_statuss>=1){
			return app('json')->fail('已经预约过了');
		}
		$today_statuss=Db::name ('an_appointment')->where ('yvyue_user_id',$request->uid())-> whereTime('ctime', $ctime)->where ('housing_id',$housing_id)->where ('status',3)->count ();
		if ($today_statuss>=1){
			return app('json')->fail('已经预约过了');
		}
		$today_statuss=Db::name ('an_appointment')-> where('ctime', $ctime)->where ('housing_id',$housing_id)->whereIn ('status',[0,1])->count ();
		if ($today_statuss>=3){
			return app('json')->fail('此时间段已经预约满了');
		}
		$data['uid']=$user_id;
		$data['housing_id']=$housing_id;
		$data['ctime']=$ctime;
		$data['yvyue_user_id']=$request->uid();
		$data['status']=0;
		Db::name ('an_appointment')->insert ($data);
		return app('json')->status('success','已提交预约，请等待');

	}
	public function an_appointment_up()
	{
		$id= input('param.id');
		$data['ctime'] = input('param.ctime');
		if (empty($data['ctime'])){
			unset($data['ctime']);
		}
		$data['status'] = input('param.status');//
		$an_appointment = Db::name ('an_appointment')->where ('id',$id)->find ();

		Db::name ('an_appointment')->where ('id',$id)->update ($data);

		return app('json')->status('success','已提交预约，请等待');

	}
	//评价
	public function appointment_add()
	{
		$id= input('param.id');
		$res =Db::name ('an_appointment')->where ('id',$id)->find ();
		if ($res['comment_times']>=date ("Y-m-d H:i:s",(time()+3600*24*7))){
			return app('json')->fail('已经评价过了');
		}
		$data['score'] = input('param.score');//综合分
		$data['content'] = input('param.content');
		$data['img_waiguan'] = input('param.img_waiguan');
		$data['img_xianchang'] = input('param.img_xianchang');
		$data['service_fen'] = input('param.service_fen');   //服务分
		$data['competence_fen'] = input('param.competence_fen');//能力分
		$data['procedure_fen'] = input('param.procedure_fen');//流程分
		$data['is_comment'] = 2;
		$data['is_name'] = input('param.is_name');
		$data['comment_time'] = date ("Y-m-d H:i:s");
		$data['comment_times'] = $res['comment_times']??date ("Y-m-d H:i:s");
		if ($data['is_name']==2){
			$data['comment_time'] =$res['comment_times']?? date ("Y-m-d H:i:s",(time()+3600*24*7)    );
		}

		Db::name ('an_appointment')->where ('id',$id)->update ($data);
		$data['label_comment_id'] = input('param.label_comment_id');
		if (!empty($data['label_comment_id'])){
			//评价标签
			$housing_label = explode (',',$data['label_comment_id']);
			$housing_label_data=[];
			foreach ($housing_label as $k=>$v){
				$housing_label_data[$k]['u_id']=$res['uid'];
				$housing_label_data[$k]['label_comment_id']=$v;
				$housing_label_data[$k]['an_app_id']=$id;
			}
			Db::name ('an_association')->insertAll ($housing_label_data);
		}
		return app('json')->status('success','评价成功');

	}
	public function appointment_del()
	{
		$id= input('param.id');
		Db::name ('an_appointment')->where ('id',$id)->update (['status'=>4]);

		return app('json')->status('success','取消预约成功');

	}
	//我的预约
	public function an_appointment_list(Request $request){
		$request_data = $request->post();

		$limit = $request_data['limit'] ?? 10;
		$page = $request_data['page'] ?? 1;

		$type = input ('type',1);
		$user_type = input ('user_type',1);

		$Model = new  \app\model\v1\housing\HousingSource();
		$data['data'] =$Model::field('h.*,a.status as an_appointment_status,a.ctime,a.id as an_id,a.is_comment')
			->alias ('h')
			->join('an_appointment a','a.housing_id = h.id')
			->where (function ($query) use ($type,$request,$user_type) {

				if ($user_type==1){
					$query->where ('a.uid',$request->uid());
				}else{
					$query->where ('a.yvyue_user_id',$request->uid());
				}
			if ($type==1){
				$query->where ('a.status','<>',4);
				$query->where ('a.ctime','>',date ('Y-m-d H:i:s',(time ()-(3600*3))));

			}elseif  ($type==2){
				$query->where ('a.status','<>',4);
				$query->where ('a.ctime','<',date ('Y-m-d H:i:s',(time ()-(3600*3))));
			}elseif  ($type==3){
				$query->where ('a.status',4);
			}elseif  ($type==4){
//				$query->where ('a.status','<>',4);
//				$query->where ('a.ctime','<',date ('Y-m-d H:i:s',(time ()-(3600*3))));
				}elseif  ($type==5){
				//
				$query->where ('a.status','<>',4);
				$query->where ('a.is_comment',2);
			}
			})
			->order ('a.id','desc')
			->page ($page,$limit)
			->field('h.*,a.status as an_appointment_status,a.ctime,a.is_comment,a.score,a.content,a.id as an_id')
			->select ();
		$data['count'] =$Model
			->alias ('h')
			->join ('an_appointment a','a.housing_id = h.id')
			->where (function ($query) use ($type,$request,$user_type) {

				if ($user_type==1){
					$query->where ('a.uid',$request->uid());
				}else{
					$query->where ('a.yvyue_user_id',$request->uid());
				}
				if ($type==1){
					$query->where ('a.status','<>',4);
					$query->where ('a.ctime','>',date ('Y-m-d H:i:s',(time ()-(3600*3))));

				}elseif  ($type==2){
					$query->where ('a.status','<>',4);
					$query->where ('a.ctime','<',date ('Y-m-d H:i:s',(time ()-(3600*3))));
				}elseif  ($type==3){
					$query->where ('a.status',4);
				}elseif  ($type==4){
//				$query->where ('a.status','<>',4);
					$query->where ('a.ctime','<',date ('Y-m-d H:i:s',(time ()-(3600*3))));
				}elseif  ($type==5){
					//
					$query->where ('a.status','<>',4);
					$query->where ('a.is_comment',2);
				}
			})		->count ();
		return app('json')->status('success','成功',$data);

	}
	//看房记录 预约记录
	public function an_appointment_history_list(Request $request){


		$request_data = $request->post();

		$limit = $request_data['limit'] ?? 10;
		$page = $request_data['page'] ?? 1;

		$where = [];

			$where ['a.uid']=$request->uid();
			$where ['a.status']='3';

		$Model = new  \app\model\v1\housing\HousingSource();
		$data['data'] =$Model::field('h.*,a.status as an_appointment_status,u.nickname as yvyuenickname,a.ctime,a.is_comment,a.score,a.content,a.id as an_id')
			->alias ('h')
			->join('an_appointment a','a.housing_id = h.id')
			->with ('street')
			->with ('MetroStation')
			->with ('HousingMatchingSourceAssociation')
			->with ('HousingHousingLabelAssociation')
			->join ('user u','u.uid =a.yvyue_user_id')
			->where ($where)
			->order ('a.id','desc')
			->page ($page,$limit)
			->select ();


		$data['count'] =$Model
			->alias ('h')
			->join ('an_appointment a','a.housing_id = h.id')
			->where ($where)
			->count ();
		return app('json')->status('success',$data);

	}
	//预约评价列表
	public function an_appointment_comment_list(Request $request){


		$request_data = $request->post();

		$limit = $request_data['limit'] ?? 10;
		$page = $request_data['page'] ?? 1;


		$where = [];
			$where ['a.uid']=$request_data['user_id'];
			$where ['a.status']='3';
			$where ['a.is_comment']='2';
			$where ['a.deltime']=null;
		$Model = new  \app\model\v1\sys\AnAppointment();
		$data['data'] =$Model->field('a.status as an_appointment_status,u.uid as  uids ,u.nickname as yvyuenickname,u.avatar as uavatar,a.ctime,a.is_comment,a.score,a.content,a.id as id,img_waiguan,img_xianchang,is_name,deltime')
			->with ('anAs')
			->alias ('a')
			->join ('user u','u.uid =a.yvyue_user_id')
			->where ($where)
			->order ('a.id','desc')
			->page ($page,$limit)
			->select ();
		$data['count'] =$Model
			->alias ('a')
			->join ('user u','u.uid =a.yvyue_user_id')
			->where ($where)
			->count ();
		return app('json')->status('success',$data);

	}
	//查询单个评价详情
	public function get_an_appointment_comment_index(Request $request){


		$request_data = $request->post();


		$result = $this->validate(
			$request_data,
			[
				'an_id' => 'require',
			],
			[
				'an_id.require' => 'ID必填',

			]
		);
		if ($result !== true) {
			return app('json')->fail($result);
		}
		$where = [];
//		$where ['a.uid']=$request->uid();
		$where ['a.id']=$request_data['an_id'];
		$Model = new  \app\lib\model\AnAppointment();
		$data['data'] =$Model->field('a.status as an_appointment_status,u.uid as  uids ,u.nickname as yvyuenickname,u.avatar as uavatar,a.ctime,a.is_comment,a.score,a.content,a.id as id,img_waiguan,img_xianchang,is_name')
			->with ('anAs')
			->alias ('a')
			->join ('user u','u.uid =a.yvyue_user_id')
			->where ($where)
			->order ('a.id','desc')
			->select ();
		return app('json')->status('success',$data);

	}
	//房东中介  看房记录   预约人 评价数量等
	public function user_yvyue_data (Request $request)
	{
		$Model = new  app\model\v1\sys\AnAppointment();

		$where ['a.uid']=$request->uid();

		$data['yvyue'] =$Model
			->alias ('a')
			->where ($where)
			->where ('a.status','<','2')
			->where ('a.ctime','>=',time ())
			->count ();
		$data['Success'] =$Model
			->alias ('a')
			->where ($where)
			->where ('a.status','3')
			->count ();

			$data['comment'] =$Model
			->alias ('a')
			->where ($where)
			->where ('deltime',null)
			->where ('a.is_comment','2')
			->count ();
		Ajaxreturn::return_succes (true,'',$data);
	}

	public function LabelComment_list()
	{
		$data['data'] = Db::name('label_comment')->order('sort','desc')->select();
		return app('json')->status('success','成功',$data);

	}
}